package cn.itcast.mybatis.dao.impl;

import cn.itcast.mybatis.dao.UserDao;
import cn.itcast.mybatis.po.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;
import java.util.Date;
import java.util.List;


public class UserDaoImplTest {
    private SqlSessionFactory sqlSessionFactory =null;

    @Before
    public void  init () throws Exception {
        String resource = "SqlMapConfig.xml";

        //第二部加载配置文件
        InputStream inputStream = Resources.getResourceAsStream(resource);
        //第三部创建sqlsessionFactory对象
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

    }

        @Test
    public void findUserById() {
        UserDao userDao = new UserDaoImpl(sqlSessionFactory);
            User user = userDao.findUserById(10);
            System.out.println(user);
        }

    @Test
    public void findUserByName() {
        UserDao userDao = new UserDaoImpl(sqlSessionFactory);
        List<User> list = userDao.findUserByName("张");
        for (User user : list) {
            System.out.println(user);
        }
    }

    @Test
    public void insertUser()  {
        UserDao userDao = new UserDaoImpl(sqlSessionFactory);
       User user = new User();
       user.setUsername("貂蝉");
       user.setAddress("米脂");
       user.setBirthday(new Date());
       user.setSex("女");
        userDao.insertUser(user);
    }


}
